<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="所属会议" prop="project_id">
                        <el-input v-model="project_code" readonly placeholder="请选择" @click="checkItem"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="工作人员" prop="salesman">
                        <el-select v-model="form.salesman" filterable clearable placeholder="请选择工作人员" style="width:100%">
                            <el-option v-for="item in workList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="岗位" prop="jobs_id">
                    <el-select v-model="form.jobs_id" filterable clearable placeholder="请选择岗位" style="width:100%" @change="josChange">
                        <el-option v-for="item in jobList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="利润" prop="profit">
                        <el-input v-model="profit" disabled placeholder="利润"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="利润率" prop="profit_margin">
                        <el-input v-model="profit_margin" disabled placeholder="利润率"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="提成比例" prop="commission_ratio">
                        <el-input v-model="form.commission_ratio" disabled placeholder="提成比例"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="提成金额" prop="commission_money">
                        <el-input v-model="form.commission_money" disabled placeholder="提成金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="创建时间" prop="date">
                    <el-input v-model="date" disabled placeholder="创建时间"></el-input>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list>
</template>

<script>
import itemList from '../list/itemList.vue';
export default {
    components: { itemList },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_id:"",
                salesman:"",
                jobs_id:"",
                commission_ratio:"",
                commission_money:"",
			},
            date:this.moment().format('YYYY-MM-DD'),
            project_code: "",
            profit: "",
            profit_margin: "",
			//验证规则
			rules: {
				enterprise: [{ required: true, message: '请输入名称' }],
			},
            dialog:{
                item:false,
            },
            workList:[],
            jobList:[],
            rateData:[]
		}
	},
	mounted() {
        this.getUser()
        this.getJobs()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.royalty.edit.post(this.form);
					} else {
						res = await this.$API.project.royalty.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.profit = data.profit
            this.profit_margin = data.profit_margin
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.project_id = data.project_id
            this.form.salesman = Number(data.salesman)
            this.form.jobs_id = data.jobs_id
            this.form.commission_ratio = data.commission_ratio
            this.form.commission_money = data.commission_money
            this.date = data.create_time
            this.project_code = data.project.project_code
            this.profit = data.project.profit
            this.profit_margin = data.project.profit_margin
		},
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.workList = res.data;
        },
        async getJobs(){
            var res = await this.$API.system.jobs.list.get();
            this.jobList = res.data;
            res.data.map(item=>{
                if(this.form.jobs_id == item.id){
                    this.rateData = item.job_rate
                }
            })
            for (let i = 0; i < this.rateData.length; i++) {
                if (this.profit_margin > this.rateData[i].big && this.profit_margin <= this.rateData[i].small) {
                    this.form.commission_ratio = this.rateData[i].rate
                }
            }
            this.form.commission_money = (Number(this.profit / 100) * this.form.commission_ratio).toFixed(2)
        },
        josChange(e){
            this.jobList.map(item=>{
                if(e == item.id){
                    this.rateData = item.job_rate
                }
            })
            for (let i = 0; i < this.rateData.length; i++) {
                if (this.profit_margin > this.rateData[i].big && this.profit_margin <= this.rateData[i].small) {
                    this.form.commission_ratio = this.rateData[i].rate
                }else if(this.rateData[i].big ==0 && this.rateData[i].small == 0){
                    this.form.commission_ratio = this.rateData[i].rate
                }
            }
            this.form.commission_money = (Number(this.profit / 100) * this.form.commission_ratio).toFixed(2)
        },
        // 项目
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.profit = data.profit
            this.profit_margin = data.profit_margin
            this.form.commission_ratio = ''
            this.form.commission_money = ''
            this.form.jobs_id = ''
        },
	}
}
</script>

<style></style>
